/**
 * Created by lwy on 2017/6/22.
 */


//关键字搜索
function kewSearch(){
    $.ajax({
        url:'/openSearchC/shopSuggest.json',
        dataType:'json',
        data:{
            keywords:$('#keyword').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                $('.connect-box').empty();
                for(var i=0;i<data.result.length;i++){
                    $('<p><img src="../img/search-icon.png" alt="搜索结果"><span>'+data.result[i]+'</span></p>').appendTo($('.connect-box'));
                }
            }
            $('.connect-box p').click(function(){
                var keywords = $(this).find('span').html();
                window.location.href=encodeURI(encodeURI("shop_list.html?keywords="+keywords+"&page=1"));
            })
        }
    })
}
//输入
document.getElementById('keyword').oninput=function(){
    if($(this).val()==""||$(this).val()=='输入您想要的内容'){
        $('.sousuo').hide();
        $('.connect-box').empty();
    }else{
        $('.sousuo').show();
        $('#keyword').css('background','#e8e8e8');
    }
    kewSearch();
}

//清除按钮清除输入框内容
$('.sousuo').click(function(){
    $('#keyword').val('');
    $('.connect-box').empty();
})

//热门搜索
$(function(){
    $.ajax({
        url:'/openSearchC/popSearch.json',
        dataType:'json',
        data:{
            city:getItem('locationCity'),
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                var data = data.datas;
                var hotSearchHtml = '';
                hotSearchHtml+='<div class="hot-search-line">';
                for(var i=0;i<3;i++){
                    hotSearchHtml+='<span>'+data[i]+'</span>';
                }
                hotSearchHtml+='</div>';
                $(hotSearchHtml).appendTo($('.hotsearch-box'))
                var hotSearchHtml2='';
                hotSearchHtml2+='<div class="hot-search-line">';
                for(var k=3;k<6;k++){
                    hotSearchHtml2+='<span>'+data[k]+'</span>';
                }
                hotSearchHtml2+='</div>';
                $(hotSearchHtml2).appendTo($('.hotsearch-box'));
                var hotSearchHtml3='';
                hotSearchHtml3+='<div class="hot-search-line">';
                for(var a=6;a<9;a++){
                    hotSearchHtml3+='<span>'+data[a]+'</span>';
                }
                hotSearchHtml3+='</div>';
                $(hotSearchHtml3).appendTo($('.hotsearch-box'))
            }
        }
    })

})

//软键盘搜索
$("#keyword").on('keypress',function(e) {
    var keycode = e.keyCode;
    var searchName = $(this).val();
    var kwdCacheKey = 'h_kwd_list';     //定义数据存储对象名
    if(keycode=='13') {
        //存储搜索记录
        var value = $("#keyword").val();
        var time = (new Date()).getTime();

        if(!value){
            alert("你未输入搜索内容");
            return false;
        }
        //输入的内容localStorage有记录
        if($.inArray(value,hisItem)>=0){
            for(var j = 0;j<localStorage.length;j++){
                if(value==localStorage.getItem(localStorage.key(j))){
                    localStorage.removeItem(localStorage.key(j));
                }
            }
            localStorage.setItem(time,value);
        }
        //输入的内容localStorage没有记录
        else{
            //由于限制了只能有6条记录，所以这里进行判断
            if(hisItem.length>4){
                firstKey = hisTime[0]
                localStorage.removeItem(firstKey);
                localStorage.setItem(time,value);
            }else{
                localStorage.setItem(time,value)
            }
        }
        init();
        //window.location.href=encodeURI(encodeURI("shop_list.html?keywords="+searchName+"&page=1"));
    }
});
var hisTime;
//从localStorage获取搜索内容的数组
var hisItem;
//从localStorage获取最早的1个搜索时间
var firstKey;
function init (){
    //每次执行都把2个数组置空
    hisTime = [];
    hisItem = [];
    //模拟localStorage本来有的记录
    //localStorage.setItem("b",12333);
    //本函数内的两个for循环用到的变量
    var i=0
    for(;i<localStorage.length;i++){
        if(!isNaN(localStorage.key(i))){
            hisTime.push(localStorage.key(i));
        }
    }

//    取到的是历史记录如果不是空格，先取再存到数组里
    if(hisTime.length>0) {
        hisTime.sort();
        for (var y = hisTime.length-1; y>0; y--) {
            localStorage.getItem(hisTime[y]).trim()&&hisItem.push(localStorage.getItem(hisTime[y]));
        }
    }

    i=0;
    //执行init(),每次清空之前添加的节点
    $(".historyBox").html("");
    for(;i<hisItem.length;i++){
        //alert(hisItem);
        $(".historyBox").prepend('<div class="history">'+hisItem[i]+'</div>')
    }
}
init();

//清除记录功能
$(".clear-history").click(function(){
    var f = 0;
    for(;f<hisTime.length;f++){
        localStorage.removeItem(hisTime[f]);
    }
    init();
    window.location.reload();
});
//苹果手机不兼容出现input无法取值以下是解决方法
$(function(){
    $('.word-break').click(function(){
        var div = $(this).text();
        $('#sec').val(div);
    })
    //取到值以后button存储无法取值，这里强迫浏览器强行刷新可解决
    $('#search').click(function(){
        window.location.reload();
    })
})
//存储数据默认隐藏获取焦点显示
$('#sec').focus(function(){
    $('.word-break').css('display','block');
})
//这里判断本地浏览器是否有离线存储记录苹果浏览器不支持localStorage.length
//  if(localStorage.length == 0) {
//    $(".Storage").html('无存储记录');
//  }
//通过DOM查找插入的记录如果有显示空反之显示无记录
if($(".delete").children().hasClass("word-break")){
    //alert("hi");
    $(".Storage").html('')
}else{
    $(".Storage").html('无存储记录');
}